﻿@page "/Events/AddPointOnClick"
@using LiveChartsCore.Defaults
@using LiveChartsCore.Drawing
@using LiveChartsCore.SkiaSharpView.Blazor
@using System.Collections.ObjectModel;
@using ViewModelsSamples.Events.AddPointOnClick

<CartesianChart
	@ref="_chart"
	Series="ViewModel.SeriesCollection"
	OnPointerDownCallback="onPointerDown"
	TooltipPosition="LiveChartsCore.Measure.TooltipPosition.Hidden">
</CartesianChart>

@code {
    // NOTE: // mark
    // BECAUSE THIS VIEWMODEL IS SHARED WITH OTHER VIEWS // mark
    // THE ViewModel.ChartUpdated, ViewModel.PointerDown and ViewModel.PointerUp METHODS // mark
    // are repeated, please ignore the viewmodel RelayCommands and use the events instead. // mark

    private CartesianChart _chart;

    public ViewModel ViewModel { get; set; } = new();

    private void onPointerDown(PointerEventArgs e)
    {
        var p = new LvcPointD(e.OffsetX, e.OffsetY);

        // scales the UI coordinates to the corresponding data in the chart.
        var dataCoordinates = _chart.ScalePixelsToData(p);

        var data = (ObservableCollection<ObservablePoint>)ViewModel.SeriesCollection[0].Values!;

        // finally add the new point to the data in our chart.
        data.Add(new ObservablePoint(dataCoordinates.X, dataCoordinates.Y));

        // You can also get all the points or visual elements in a given location.
        var points = _chart.GetPointsAt(new LvcPoint(p.X, p.Y));
        var visuals = _chart.GetVisualsAt(new LvcPoint(p.X, p.Y));
	}
}
